package com.accesshollywood.email;

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.accesshollywood.indexables.User;
import com.accesshollywood.trace.Trace;

public class Email {
	private static final boolean	EMAILS_ON		= true;
	// String host = "UCTMLVEM05.e2k.ad.ge.com";
	// String username = "NBCUNI\\500441547:rich.helvey@nbcuni.com";

	private static final String		host			= "UCTMLVEM02.e2k.ad.ge.com";
	private static final String		username		= "NBCUNI\\501380708:AH.Graphics@nbcuni.com";
	private static final String		from			= "AH.Graphics@nbcuni.com";
	private static final String		password		= "Pa55word";

	private static Session			session;
	private static Transport		transport		= null;

	// InternetAddress[] to; // = { new InternetAddress("rich.helvey@nbcuni.com"), new InternetAddress("gordon.woo@nbcuni.com") };

	private static final String		subjectHeader	= "TESTING... [ahGrfx] ";

	public Email() {
	}

	public static void setup() {

		Properties props = new Properties();
		props.setProperty("mail.host", host);
		props.setProperty("mail.user", username);
		props.setProperty("mail.password", password);

		String host = props.getProperty("mail.smtp.host");
		if (host == null) {
			host = props.getProperty("mail.host");
			if (host == null) {
				Trace.error("No \"mail.smtp.host\" or " + "\"mail.host\" specified, skipping SMTP test.");
				return;
			}
		}
		String user = props.getProperty("mail.smtp.user");
		if (user == null) {
			user = props.getProperty("mail.user");
			if (user == null) {
				Trace.error("No \"mail.smtp.user\" or " + "\"mail.user\" found, skipping SMTP test.");
				return;
			}
		}
		String password = props.getProperty("mail.smtp.password");
		if (password == null) {
			password = props.getProperty("mail.password");
			if (user == null) {
				Trace.error("No \"mail.smtp.password\" or " + "\"mail.password\" found, skipping SMTP test.");
				return;
			}
		}
		session = Session.getDefaultInstance(props);

		try {
			transport = session.getTransport("smtp");
			transport.connect(host, username, password);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	public static void sendEmail(User toUser, String subject, String body) {
		if (EMAILS_ON && toUser.getEmailAddress() != null) {
			try {
				InternetAddress fromAddress = new InternetAddress(from);
				InternetAddress[] toAddress = { new InternetAddress(toUser.getEmailAddress()) };

				MimeMessage msg = new MimeMessage(session);
				msg.setSubject(subjectHeader + subject);
				msg.setText(body);

				msg.setFrom(fromAddress);
				msg.setRecipients(Message.RecipientType.TO, toAddress);
				transport.sendMessage(msg, toAddress);
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		}
	}

	public static void sendEmail(ArrayList<User> toUsers, String subject, String body) {
		if (EMAILS_ON) {
			ArrayList<InternetAddress> toAddress = new ArrayList<InternetAddress>();// { new InternetAddress(toUser.emailAddress) };

			for (User toUser : toUsers) {
				if (toUser.getEmailAddress() != null) {
					try {
						toAddress.add(new InternetAddress(toUser.getEmailAddress()));
					} catch (AddressException e) {
						e.printStackTrace();
					}
				}
			}

			InternetAddress[] toArray = new InternetAddress[toAddress.size()];
			toAddress.toArray(toArray);

			try {
				InternetAddress fromAddress = new InternetAddress(from);

				MimeMessage msg = new MimeMessage(session);
				msg.setSubject(subjectHeader + subject);
				msg.setText(body);

				msg.setFrom(fromAddress);
				msg.setRecipients(Message.RecipientType.TO, toArray);
				transport.sendMessage(msg, toArray);
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		}
	}
}
